package cn.zhoutao.user.service.impl;

import cn.zhoutao.basic.constants.BaseConstant;
import cn.zhoutao.basic.dto.RegisterDto;
import cn.zhoutao.basic.exception.BusinessException;
import cn.zhoutao.basic.util.MD5Utils;
import cn.zhoutao.basic.util.StrUtils;
import cn.zhoutao.user.domain.Logininfo;
import cn.zhoutao.user.domain.User;
import cn.zhoutao.user.mapper.LogininfoMapper;
import cn.zhoutao.user.mapper.UserMapper;
import cn.zhoutao.user.service.IUserService;
import cn.zhoutao.basic.service.impl.BaseServiceImpl;
import com.mysql.jdbc.UpdatableResultSet;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrZhou
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();

        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //参数空校验
        if (StringUtils.isEmpty(smsCode) ||StringUtils.isEmpty(password) ||StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("内容不能为空！");
        }
        //两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }
        //短信验证码判断
        String key = BaseConstant.RegisterConstants.REGISTER_SMS_PREFIX + phone;
        Object smsCodeTmp = redisTemplate.opsForValue().get(key);

        //验证码过期，重新获取
        if (smsCodeTmp==null){
            throw new BusinessException("短信验证码已过期，请重新获取！");
        }
        //拿到了验证码，验证码是否一致
        if (!smsCode.equalsIgnoreCase(smsCodeTmp.toString().split(":")[0])){
        //不一致的话，验证码错误，请核对
            throw new BusinessException("您输入的验证码不正确，请核对！");
        }

        //一致就往下走
        //保存数据 t_user和t_loginfo
        User user = dto2user(dto);//把dto转化成user对象
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);//同名拷贝原则
        logininfo.setType(1);//必须手动设置type
        return logininfo;
    }

    private User dto2user(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    /**
     * userde cud都需要级联操作t_logininfo
     */
    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }


}
